// pages/service_more/service_more.js
//获取应用实例
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog"

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabbar: {},
        filterShow: false,
        filterList: [],
        shopList: [],
        pageNumber: 1,
        pageSize: 10,
        isRefresh: true,
        searchType: '',
        searchName: '',
        isLoading: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.editTabBar();
        this.setData({
            theme: app.globalData.theme,
            tabbar: app.globalData.tabBar
        })
        wx.setNavigationBarTitle({
            title: "社区服务"
        })
    },

    /**
     * 获取筛选类型
     */
    getFilterList: function () {
        let list = [{
            flag: '',
            name: '',
            dicKey: '发现周边类型',
            dicValue: '全部',
            isSelect: true
        }]
        http.get("dic/open/getDicByName", {
            name: "D0041"
        }).then(res => {
            if (res.data.code === http.REQUEST_OK) {
                res.data.data.forEach(item => {
                    item.isSelect = false
                    list.push(item)
                })
                this.setData({
                    filterList: list
                })
            } else {
                this.setData({
                    filterList: list
                })
            }
        }, err => {
            this.setData({
                filterList: list
            })
        })
    },

    /**
     * 获取商家列表
     */
    getShopList: function () {
        this.setData({
            isLoading: true
        })
        const _this = this
        let data = {
            area_code: app.globalData.areaCode,
            page_number: this.data.pageNumber,
            page_size: this.data.pageSize
        }
        log.e("类型:" + this.data.searchType + "问问" + this.data.searchName)
        if (this.data.searchType != '') {
            data.type = this.data.searchType
        } else {
            data.type = ''
        }
        if (this.data.searchName != '') {
            data.name = this.data.searchName
        }
        http.get("around/page", data).then(res => {
            if (res.data.code === http.REQUEST_OK) {
                res.data.data.list.forEach(item => {
                    _this.data.shopList.push(item)
                })
                this.setData({
                    shopList: _this.data.shopList
                })
                setTimeout(() => {
                    _this.setData({
                        isRefresh: false
                    })
                    Toast.clear()
                }, 500)
            } else {
                Toast.clear()
                Dialog.alert({
                    title: "获取商家失败",
                    message: res.data.msg
                }).then(() => {

                })
                setTimeout(() => {
                    _this.setData({
                        isRefresh: false
                    })
                }, 500)
            }
            _this.setData({
                isLoading: false
            })
        }, err => {
            Toast.fail("网络异常")
            this.setData({
                isRefresh: false,
                isLoading: false
            })
        })
    },

    /**
     * 商家详情
     * @param e
     */
    onShopDetails: function (e) {
        const id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: "./service_detail/service_detail?id=" + id
        })
    },

    /**
     * 获取搜索内容
     * @param e
     */
    onSearchInput: function (e) {
        const name = e.detail.value
        this.setData({
            searchName: name
        })
    },

    /**
     * 开始搜索
     * @param e
     */
    onSearch: function (e) {
        this.data.pageNumber = 1
        this.data.shopList = []
        this.getShopList()
    },

    /**
     * 下拉刷新
     * @param e
     */
    onRefresh: function (e) {
        this.data.pageNumber = 1
        this.data.shopList = []
        this.getShopList()
    },

    /**
     * 上拉加载
     * @param e
     */
    onLower: function (e) {
        this.data.pageNumber += 1
        Toast.loading({
            duration: 0,
            message: '加载中...',
            forbidClick: true,
        });
        this.getShopList()
    },

    /**
     * 筛选弹出层
     * @param e
     */
    onFilterShow: function (e) {
        this.setData({
            filterShow: !this.data.filterShow
        })
    },

    /**
     * 选择服务
     * @param e
     */
    onSelectService: function (e) {
        const index = e.currentTarget.dataset.index
        const flag = e.currentTarget.dataset.flag
        this.data.filterList.forEach((item, i) => {
            item.isSelect = index == i;
        })
        this.setData({
            searchType: flag,
            filterList: this.data.filterList,
            filterShow: false
        })
        this.data.pageNumber = 1
        this.data.shopList = []
        this.getShopList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.data.shopList = []
        this.getShopList()
        this.getFilterList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.setData({
            searchType: '',
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})